﻿@using CoralNode.Core.Entity.Enums
@using CoralNode.MvcClient.Helper
@using Coralcode.Adapter.Quiz.Entity.Domain
@using Coralcode.Adapter.Quiz.Entity.Domain
@using Coralcode.Adapter.Quiz.Entity.Enums
@using Coralcode.Utils.Basic
@using Newtonsoft.Json
@model Coralcode.Adapter.Quiz.Entity.Domain.QuizDomain
@{
    ViewData["curr"] = "test";
    int type = Model.Type;
}
<!--left-->
@Html.IncludePage("CoralAdmin/Node", "_Left")
<div id="pbody">
    <div class="extra">
        <div class="pbody_top">
            <span class="ico ico_setting_system"></span><span class="t t_c">题目管理&nbsp;&nbsp;/&nbsp;&nbsp;<b
                class=" t_small f_no">添加题目</b></span>
        </div>
        <div id="main">
            <div class="funsearch p_1 pl_2 pr_2 extra_normal border-bottom1">
                <a class="btn_2 ml_1 btn j_add_option">添加选项</a>
                <a href="/m/quiz/listQuiz?nodeId=@Model.NodeId&type=@type" class="ico ico_back btn f_r mt_0-5"></a>
            </div>
            <form id="spform" action="/m/quiz/CreateQuiz?nodeId=@Model.NodeId&type=@type" method="post" enctype="multipart/form-data">
                @if (@ViewBag.Msg != null)
                {
                    <div class="error_msg">
                        @ViewBag.Msg.Msg
                    </div>
                }
                <div class="pt_1 slideswitch pl_2 pr_2" data-js="slideswitch">
                    <div class="switch_tab">
                        <span class="btn"><i class="ico ico_slideswitch_show"></i></span>主要信息
                    </div>
                    <div class="table_form form_main switch_cnt">
                        <div class="pl_2">
                            @Html.AntiForgeryToken()
                            @Html.ValidationSummary(true)
                            <table>
                                <tbody>
                                    <tr>
                                        <th width="100">题目<span class="rq">*</span></th>
                                        <td>
                                            @Html.TextBoxFor(m => m.Title, new { @class = "text w_37 required" })
                                            @Html.HiddenFor(n => n.NodeId)
                                            @Html.HiddenFor(n => n.Type)
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>选项</th>
                                        <td class="j_copy_td"></td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <div class="submitbox p_2 extra_normal">
                    <input type="hidden" name="Options" id="DataSet" />
                    <input type="submit" class="btn_1" value="提    交" />
                </div>
            </form>
        </div>
    </div>
</div>
<div class="j_copy" style="display: none">
    @if (type == QuizTypeEnum.Point)
    {
        <div class="clearfix mb_1-5 option j_option" type="@QuizTypeEnum.Point">
            <input type="text" class="text w_37" name="Question" />
             <span class="l mr_1">配图</span>
            <input type="hidden" name="Thumb"/>
            <ul class="list_img" href="/m/common/upload" data-js="listimg">
                <li class="btn li_add js_openuploader" data-meta="{data:'{module:\'quize\'}',multi:false,method:'add',type:'image'}">
                    <span class="ico ico_upload_image"></span>
                </li>
            </ul>
            <span class="l mr_1 ml_3">分数</span>
            <input type="text" class="text w_21" name="Weight" value="0" />
            <a href="#" class="j_delete_option">删除本条</a>
        </div>
    }
    else if (type == QuizTypeEnum.Link)
    {
        <div class="clearfix mb_1-5 option j_option" type="@QuizTypeEnum.Link">
            <input type="text" class="text w_37" name="Question" />
             <span class="l mr_1">配图</span>
            <input type="hidden" name="Thumb"/>
            <ul class="list_img" href="/m/common/upload" data-js="listimg">
                <li class="btn li_add js_openuploader" data-meta="{data:'{module:\'quize\'}',multi:false,method:'add',type:'image'}">
                    <span class="ico ico_upload_image"></span>
                </li>
            </ul>
            <span class="l mr_1 ml_3">分数</span>
            <input type="text" class="text w_21" name="Weight" value="0" />
            <span class="l mr_1">跳转至</span>
            <select class="j_combobox" name="LinkQuizId">
                <option value="0">空</option>
                @foreach (var item in ViewBag.QuizList)
                {
                    <option value="@item.Id">@item.Title</option>
                }
            </select>
            <a href="#" class="j_delete_option pl_5">删除本条</a>
        </div>
    }
    else
    {
        <div class="clearfix mb_1-5 option j_option" type="@QuizTypeEnum.Stat">
            <input type="text" class="text w_37" name="Question" />
             <span class="l mr_1">配图</span>
            <input type="hidden" name="Thumb"/>
            <ul class="list_img" href="/m/common/upload" data-js="listimg">
                <li class="btn li_add js_openuploader" data-meta="{data:'{module:\'quize\'}',multi:false,method:'add',type:'image'}">
                    <span class="ico ico_upload_image"></span>
                </li>
            </ul>
            <span class="l mr_1 ml_3">答案</span>
            <select class="j_combobox" name="AnswerId">
                <option value="0">空</option>
                @foreach (var item in ViewBag.AnswerList)
                {
                    <option value="@item.Id">@item.Title</option>
                }
            </select>
            <a href="#" class="j_delete_option pl_5">删除本条</a>
        </div>
    }
</div>
<script>
    $(function () {
        $('.j_add_option').click(function () {
            var $option = $('.j_copy div.j_option').clone().appendTo('.j_copy_td');
            $option.find(".j_combobox").combobox();
        });
        $('.j_delete_option').live("click", function (e) {
            e.preventDefault();
            $(this).parent().remove();
        });
        $('#spform').submit(function (e) {
            var dataset = new Array();
            $('.j_copy_td div.option').each(function (i, n) {
                var type = $(n).attr("type");
                switch (type) {
                    case "1":
                        var option = encodeURIComponent($(n).find('input[name="Question"]').val());
                        var weight = $(n).find('input[name="Weight"]').val();
                        var thumb = encodeURIComponent($(n).find('input[name="Thumb"]').val());
                        dataset.push('{"Thumb":"'+thumb+'","Question":"' + option + '","Weight":"' + weight + '"}');
                        break;
                    case "2":
                        var option2 = encodeURIComponent($(n).find('input[name="Question"]').val());
                        var weight2 = $(n).find('input[name="Weight"]').val();
                        var thumb2 =encodeURIComponent($(n).find('input[name="Thumb"]').val());
                        var linkQuizId = $(n).find('select[name="LinkQuizId"]').val();
                        dataset.push('{"Thumb":"' + thumb2 + '","Question":"' + option2 + '","Weight":"' + weight2 + '","LinkQuizId":"' + linkQuizId + '"}');
                        break;
                    default:
                        var option3 = encodeURIComponent($(n).find('input[name="Question"]').val());
                        var thumb3 = encodeURIComponent($(n).find('input[name="Thumb"]').val());
                        var answer = $(n).find('select[name="AnswerId"]').val();
                        dataset.push('{"Thumb":"' + thumb3 + '","Question":"' + option3 + '","AnswerId":"' + answer + '"}');
                }

            });
            $('#DataSet').val("[" + dataset + "]");
        });
    });
</script>

